#!/usr/bin/env python
# coding: utf-8
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

if ((defined(ark_standalone_build) && ark_standalone_build) ||
    (defined(ark_static_standalone_build) && ark_static_standalone_build)) {
  import("//arkcompiler/runtime_core/static_core/ark_config.gni")
} else {
  import(
      "//build/config/components/runtime_core/static_core/ark_common_config.gni")
}

if (defined(ohos_indep_compiler_enable) && ohos_indep_compiler_enable) {
  typescript_dir =
      get_label_info("typescript:build_typescript_etc", "target_out_dir")
  typescript_tgz = "${typescript_dir}/libs/ohos-typescript-4.9.5-r4.tgz"
} else {
  typescript_dir = get_label_info("//third_party/typescript:build_typescript",
                                  "target_out_dir")
  typescript_tgz = "${typescript_dir}/ohos-typescript-4.9.5-r4.tgz"
}

action("build_arkguard") {
  sources = [
    "package.json",
    "src/ArkObfuscator.ts",
    "src/ArkObfuscatorForTest.ts",
    "src/IObfuscator.ts",
    "src/cli/SecHarmony.ts",
    "src/common/ApiExtractor.ts",
    "src/common/ApiReader.ts",
    "src/common/type.ts",
    "src/configs/INameObfuscationOption.ts",
    "src/configs/IOptions.ts",
    "src/configs/preset/es_reserved_properties.json",
    "src/generator/INameGenerator.ts",
    "src/generator/NameFactory.ts",
    "src/generator/OrderedNameGenerator.ts",
    "src/transformers/TransformPlugin.ts",
    "src/transformers/TransformerManager.ts",
    "src/transformers/layout/DisableConsoleTransformer.ts",
    "src/transformers/rename/RenameFileNameTransformer.ts",
    "src/transformers/rename/RenameIdentifierTransformer.ts",
    "src/transformers/rename/RenamePropertiesTransformer.ts",
    "src/transformers/rename/ShorthandPropertyTransformer.ts",
    "src/utils/FileUtils.ts",
    "src/utils/ListUtil.ts",
    "src/utils/NameCacheUtil.ts",
    "src/utils/NodeUtils.ts",
    "src/utils/OhsUtil.ts",
    "src/utils/ScopeAnalyzer.ts",
    "src/utils/SourceMapUtil.ts",
    "src/utils/TransformUtil.ts",
    "src/utils/TypeUtils.ts",
  ]

  external_deps = [ "typescript:build_typescript_etc" ]
  script = "compile_arkguard.py"
  args = [
    rebase_path(get_path_info("./", "abspath")),
    rebase_path("$typescript_tgz"),
    rebase_path("${target_out_dir}"),
  ]

  outputs = [ "${target_out_dir}/arkguard-1.1.3.tgz" ]
}

if (!(defined(ark_static_standalone_build) && ark_static_standalone_build)) {
  ohos_shared_headers("build_arkguard_etc") {
    include_dirs = []
    deps = [ ":build_arkguard" ]
    part_name = "ets_frontend"
    subsystem_name = "arkcompiler"
  }
}
